home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 2.0.1 / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 06 / UIconEdit.inc1.p < prev    next >
Text File  |  1990-10-25  |  7KB  |  261 lines

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.  
  14.  
  15. TYPE
  16.     LongArrayHdl =        ^LongArrayPtr;
  17.     LongArrayPtr =        ^LongArray;
  18.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  19.  
  20.  
  21.  
  22. {-------------------------------------------------------------------------------------------}
  23. {--------------------------------TIconApplication methods-----------------------------------}
  24. {-------------------------------------------------------------------------------------------}
  25.  
  26. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  27.  
  28. BEGIN
  29.     IApplication(iconFileType);
  30. END;
  31.  
  32.  
  33.  
  34. {-------------------------------------------------------------------------------------------}
  35.  
  36. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  37.  
  38. VAR
  39.     anIconDocument:        TIconDocument;
  40.  
  41. BEGIN
  42.     New(anIconDocument);                            { Create a TIconDocument object.        }
  43.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  44.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  45.  
  46.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  47. END;
  48.     
  49.     
  50.     
  51.     
  52. {-------------------------------------------------------------------------------------------}
  53. {----------------------------------TIconDocument methods------------------------------------}
  54. {-------------------------------------------------------------------------------------------}
  55.  
  56. PROCEDURE TIconDocument.IIconDocument;
  57.  
  58. VAR anIconBitMap : TIconBitMap;
  59.  
  60. BEGIN
  61.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  62.                                                     { fails, TIconDocument.Free works okay.    }
  63.  
  64.     IDocument(kFileType,                             { This document's file type.            }
  65.               kSignature,                             { This document's creator.                }
  66.               kUsesDataFork,                         { This document does use the data fork    }
  67.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  68.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  69.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  70.  
  71.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  72.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  73.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  74.     
  75.     fIconBitMap := anIconBitMap;                    { Store the reference in a field.        }
  76. END;
  77.  
  78.  
  79.  
  80. {-------------------------------------------------------------------------------------------}
  81.  
  82. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  83.  
  84.  
  85. BEGIN
  86.     fIconBitMap.Clear;
  87. END;
  88.  
  89.  
  90.  
  91. {-------------------------------------------------------------------------------------------}
  92.  
  93. PROCEDURE TIconDocument.Free; OVERRIDE;
  94.     
  95. BEGIN
  96.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  97.  
  98.     INHERITED Free;
  99. END;
  100.  
  101.  
  102.  
  103. {-------------------------------------------------------------------------------------------}
  104.  
  105. PROCEDURE TIconDocument.DoMakeViews(forPrinting: boolean); OVERRIDE;
  106.  
  107. BEGIN
  108.     { Do nothing for now. }
  109. END;
  110.  
  111.  
  112.  
  113.         
  114. {-------------------------------------------------------------------------------------------}
  115. {-----------------------------------TIconBitMap methods-------------------------------------}
  116. {-------------------------------------------------------------------------------------------}
  117.  
  118. PROCEDURE TIconBitMap.IIconBitMap;
  119.  
  120. BEGIN
  121.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  122.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  123. END;
  124.  
  125.  
  126.  
  127. {-------------------------------------------------------------------------------------------}
  128.  
  129. PROCEDURE TIconBitMap.Free; OVERRIDE;
  130.  
  131. BEGIN
  132.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  133. END;
  134.  
  135.  
  136.  
  137. {-------------------------------------------------------------------------------------------}
  138.  
  139. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  140.  
  141. BEGIN
  142.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  143.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  144. END;
  145.             
  146.             
  147.  
  148. {-------------------------------------------------------------------------------------------}
  149.  
  150. PROCEDURE TIconBitMap.Clear;
  151.  
  152. VAR
  153.     iconAsLongArray:    LongArrayHdl;
  154.     i:                    INTEGER;
  155.  
  156. BEGIN
  157.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  158.  
  159.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  160.         iconAsLongArray^^[i] := 0;
  161. END;
  162.  
  163.  
  164.  
  165. {-------------------------------------------------------------------------------------------}
  166.  
  167. PROCEDURE TIconBitMap.Invert;
  168.  
  169. VAR
  170.     iconAsLongArray:    LongArrayHdl;
  171.     i:                    INTEGER;
  172.  
  173. BEGIN
  174.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  175.  
  176.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  177.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  178. END;
  179.  
  180.  
  181.  
  182. {-------------------------------------------------------------------------------------------}
  183.  
  184. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  185.     { This converts the given icon bit to a word and bit in an array of long words. }
  186.  
  187. VAR
  188.     bitNumber:        INTEGER;
  189.  
  190. BEGIN
  191.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  192.     word := bitNumber DIV 32;
  193.     bit := 31 - (bitNumber MOD 32);
  194. END;
  195.  
  196.  
  197.  
  198. {-------------------------------------------------------------------------------------------}
  199.  
  200. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  201.     { Returns the state of the given bit in the icon being drawn. }
  202.  
  203. VAR
  204.     word:            INTEGER;
  205.     bitInWord:        INTEGER;
  206.  
  207. BEGIN
  208.     IconBitToWordBit(iconBit, word, bitInWord);
  209.  
  210.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  211. END;
  212.  
  213.  
  214.  
  215. {-------------------------------------------------------------------------------------------}
  216.  
  217. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  218.     { Set the state of the given bit in the icon being drawn. }
  219.  
  220. VAR
  221.     word:            INTEGER;
  222.     bitInWord:        INTEGER;
  223.  
  224. BEGIN
  225.     IconBitToWordBit(iconBit, word, bitInWord);
  226.  
  227.     {$H-}                                            { So the compiler thinks this is unsafe.}
  228.     IF turnBitOn THEN
  229.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  230.     ELSE
  231.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  232.     {$H+}
  233. END;
  234.  
  235.  
  236.  
  237. {-------------------------------------------------------------------------------------------}
  238.  
  239. FUNCTION TIconBitMap.Copy: TIconBitMap;
  240.  
  241. VAR
  242.     copyOfIcon:        TIconBitMap;
  243.  
  244. BEGIN
  245.     New(copyOfIcon);                                { Create a TIcon object.                }
  246.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  247.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  248.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  249.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  250. END;
  251.  
  252.  
  253.             
  254. {-------------------------------------------------------------------------------------------}
  255.  
  256. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  257.  
  258. BEGIN
  259.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  260. END;
  261.